class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        int n=nums.size();
        int mymax=-1;
        for(int i=0;i<n;++i)
        {
            mymax=max(mymax,nums[i]);
        }
        vector<int> v(mymax+1,0);
        for(auto ch:nums)
        {
            v[ch]+=ch;
        }
        vector<int> f(mymax+1,0);
        vector<int> g(mymax+1,0);
        f[0]=v[0];
        g[0]=0;
        for(int i=1;i<v.size();++i)
        {
            f[i]=g[i-1]+v[i];
            g[i]=max(g[i-1],f[i-1]);
        }

        return max(f[mymax],g[mymax]);
    }
};
